import express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
import cors from 'cors';

const app = express();

// 启用CORS
app.use(cors());

// 代理配置
app.use('/api', createProxyMiddleware({
  target: 'http://10.59.9.18:4999',
  changeOrigin: true,
  pathRewrite: {
    '^/api': '/api', // 保持路径不变
  },
  onProxyReq: (proxyReq, req, res) => {
    console.log('代理请求:', req.method, req.url);
  },
  onProxyRes: (proxyRes, req, res) => {
    console.log('代理响应:', proxyRes.statusCode, req.url);
  },
  onError: (err, req, res) => {
    console.error('代理错误:', err.message);
    res.status(500).json({ error: '代理服务器错误' });
  }
}));

const PORT = 3001;
app.listen(PORT, () => {
  console.log(`代理服务器运行在 http://localhost:${PORT}`);
  console.log(`代理目标: http://10.59.9.18:4999`);
});
